package Solutions;

import javax.xml.crypto.dsig.spec.HMACParameterSpec;
import java.util.Arrays;

public class Solution4 {
    public static void main(String[] args) {
        String str="今晚吃烧烤!";
        Solustion solustion=new Solustion();
         String massage =solustion.Exchange("今 晚 上 吃 烧 烤 ！");
        System.out.println(massage);
    }
}
class Solustion{
    public String  Exchange(String str) {
        str = str.trim();                               // 删除首尾空格
        int j = str.length() - 1, i = j;
        StringBuilder res = new StringBuilder();
        while (i >= 0) {
            while (i >= 0 && str.charAt(i) != ' ') i--;     // 搜索首个空格
            res.append(str.substring(i + 1, j + 1) + " ");  // 添加单词
            while (i >= 0 && str.charAt(i) == ' ') i--;     // 跳过单词间空格
            j = i;                                              // j 指向下个单词的尾字符
        }
        return res.toString().trim();

    }
}
